import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/constants/constant.dart';
import 'package:flutter_admin/models/odoo/udt_tb_lendrecord.dart';
import 'package:flutter_admin/pages/common/date_time_select.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:intl/intl.dart';
import 'package:quiver/strings.dart';
import '../../../generated/l10n.dart';

class UdtTbLendrecordDialog extends StatefulWidget {
  UdtTbLendrecordDialog();

  @override
  _UdtTbLendrecordDialogState createState() => _UdtTbLendrecordDialogState();
}

class _UdtTbLendrecordDialogState extends State<UdtTbLendrecordDialog> {

  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  UdtTbLendrecord  udtTbLendrecord = UdtTbLendrecord();

   //文档类别
  late List<SelectOptionVO> docmenttypeIdList = [];
   //借阅人员名称
  late List<SelectOptionVO> lenduserIdList = [];

  DateFormat format= DateFormat('yyyy-MM-dd');
  @override
  void initState() {
    var department = StoreUtil.read(Constant.KEY_CURRENT_USER_INFO)[0]["department_id"];
    udtTbLendrecord.departmentId = department[0].toString();
    udtTbLendrecord.departmentName = department[1].toString();
    super.initState();
    _loadData();
  }

  _loadData() async{
    //文档类别
    docmenttypeIdList = await DynamicDataApi.getModelSelect(active:false,model: "dms.documenttype",name: "documenttypename",key: "department_id",val: int.parse(udtTbLendrecord.departmentId!));
    setState(() { });
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelectDate(
            context,
            label: '借阅开始时间',
            value: udtTbLendrecord.lendstartdate,
            lastDate: isNotBlank(udtTbLendrecord.lendenddate)?format.parse(udtTbLendrecord.lendenddate!):null,
            onDateChange: (v) {
              formKey.currentState?.save();
              udtTbLendrecord.lendstartdate = v;
              setState(() {});
            },
            onSaved: (v) {
              udtTbLendrecord.lendstartdate = v;
            },
          ),
          CrySelectDate(
            context,
            label: '借阅结束时间',
            value: udtTbLendrecord.lendenddate,
            firstDate: isNotBlank(udtTbLendrecord.lendstartdate)?format.parse(udtTbLendrecord.lendstartdate!):null,
            onDateChange: (v) {
              formKey.currentState?.save();
              udtTbLendrecord.lendenddate = v;
              setState(() {});
            },
            onSaved: (v) {
              udtTbLendrecord.lendenddate = v;
            },
          ),
          CryInput(
            label: '部门',
            value: udtTbLendrecord.departmentName,
            enable: false,
          ),
          CrySelect(
            label: '档案项目',
            dataList: docmenttypeIdList,
            value: udtTbLendrecord.documenttypeId,
            onSaved: (v) => { udtTbLendrecord.documenttypeId = v},
          ),
          CryInput(
            label: '档案盒编号',
            value: udtTbLendrecord.documentno,
            onSaved: (v) {
              udtTbLendrecord.documentno = v;
            },
          ),
          CryInput(
            label: '借阅人',
            value: udtTbLendrecord.lenduserName,
            onSaved: (v) => { udtTbLendrecord.lenduserName = v},
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, udtTbLendrecord);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}